package cn.edu.lzzy.demoMVC;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BookController {
    // http://127.0.0.1:8080/hello?name=小黑子
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown book") String name) {
        return "Hello " + name;
    }

    // http://127.0.0.1:8080/book
    @RequestMapping("/book")
    @ResponseBody
    public Book book() {
        Book book = new Book();
        book.setName("ikun养成计划");
        book.setPrice(2.5);
        return book;
    }

    // http://127.0.0.1:8080/save_book?name=ikun养成计划&price=2.5
    @RequestMapping("/save_book")
    @ResponseBody
    public String saveBook(Book b) {
        return "book will save：name=" + b.getName() + " , price" + b.getPrice();
    }

    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String html(){
        return "index.html";
    }

    @ModelAttribute
    public void parseBook(@RequestParam(name = "name", defaultValue = "unknown book") String name
            , @RequestParam(name = "price", defaultValue = "666") double price, Book book) {
        book.setName("ikun养成计划");
        book.setPrice(2.5);
    }
}
